package P_2023_03_05_String;

import java.util.Scanner;

public class StringTest {
    public static void main(String[] args) {
        /*//创建对象--传递一个字符数组，根据字符数组的内容再创建一个心得字符串对象
        char[] chs = {'a', 'b', 'c'};
        String s1 = new String(chs);
        System.out.println(s1);

        String s2 = new String();
        String s3 = "";
        System.out.println("@" + s2 + "!");
        System.out.println("@" + s3 + "!");

        //传递一个字节型数组，根据字节数组的内容再创建一个心得字符串对象
        //应用对象--网络中数据的传输都是字节信息
        byte[] bytes = {97, 98, 100, 102};
        String s4 = new String(bytes);
        System.out.println(s4);*/

        //1.用户登录界面---三次机会
        //test1();

        //2.输入一个字符串，输出大写字母，小写字母，数字出现的个数
        //test2();

        //3.把数组变成字符串并返回
        //test3();

        //4.字符串反转
        test4();


    }

    //1.test
    public static void test1() {
        String rightUserName = "zhuZhiQue";
        String rightPassword = "123456";

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String userName = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();

            if (rightUserName.equals(userName) && rightPassword.equals(password)) {
                System.out.println("登陆成功！");
            } else {
                if (i == 2) {
                    System.out.println("用户：" + rightUserName + "已被锁定，请联系客服！");
                    break;
                }
                System.out.println("账号或密码错误！您还剩" + (2 - i) + "次机会！");
            }
        }
    }

    //2.test
    public static void test2() {
        //1.输入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str = sc.next();

        //2.统计个数
        int bigCount = 0;
        int smallCount = 0;
        int NumberCount = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c > 'a' && c < 'z') {
                smallCount++;
            } else if (c > 'A' && c < 'Z') {
                bigCount++;
            } else if (c > '0' && c < '9') {
                NumberCount++;
            }
        }
        System.out.println("大写字母有：" + bigCount + "个。");
        System.out.println("小写字母有：" + smallCount + "个。");
        System.out.println("数字有：" + NumberCount + "个。");

    }

    //3.把数组变成字符串并返回
    public static void test3() {
        int[] arr = new int[]{1, 2, 3, 4};
        String str = arrToString(arr);
        System.out.println(str);
    }

    public static String arrToString(int[] arr) {
        if (arr == null) {
            return "";
        }

        if (arr.length == 0) {
            return "[]";
        }

        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                str += arr[i];
            } else {
                str = str + arr[i] + ", ";
            }
        }
        str += "]";
        return str;
    }

    //4.字符串反转
    public static void test4(){
        String str = reverse("abcde");
        System.out.println(str);
    }
    public static String reverse(String str){
        String result = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            char c = str.charAt(i);
            result += c;
        }
        return result;
    }

}
